import React, { useState, useRef } from 'react'
import {message} from 'antd'
import { PageHeaderWrapper } from '@ant-design/pro-layout'
import ProTable, { ProColumns, ActionType } from '@ant-design/pro-table'
import { connect} from 'umi'
import { querySettlementList,updateDimissionSettlement,sendSettlementBpm} from '../service'
import { BPMStatusEnum } from "@/utils/constant";
import moment from 'moment'
import SettlementForm from './components/SettlementForm'
import store from 'store2';

const TableList = () => {
  const actionRef = useRef();
  const [visible, setVisible] = useState(false);
  const [loading,setLoading] = useState(false);
  const [record,setRecord] = useState(null);

  const settleAccounts = (record) =>{
    setVisible(true);
    setRecord(record);
  };

  const handleCancel = () => {
    setVisible(false);
    setRecord(null);
  };

  const handleSubmit = (values) => {
    setLoading(true);
    updateDimissionSettlement(values).then((res)=>{
      if(res.flag){
        store.remove(`personnel-resign-settlement-` + record.id);
        sendSettlementBpm(record.id).then(res => {
          if(res.flag){
            handleCancel();
            message.success('离职结算操作成功！');
            window.open(res.data);
            actionRef.current.reload();
          }else{
            message.error(`发起bpm错误:`+res.error);
          }
        })
      }else{
        message.error(res.error);
      }
      setLoading(false)
    })
  };


  const columns = [
    {
      title: '序号',
      render:(text,record,index)=>`${index+1}`,
      hideInSearch: true,
    },
    {
      title: '工号',
      hideInSearch: true,
      dataIndex: 'code',
      key:'code',
    },
    {
      title: '姓名',
      dataIndex: 'name',
      hideInSearch: true,
      key:'name'
    },
    {
      title: '公司',
      dataIndex: 'orgname',
      hideInSearch: true,
      key:'orgname'
    },
    {
      title: '部门',
      hideInSearch: true,
      dataIndex: 'unitname',
      key:'unitname'
    },
    {
      title: '岗位',
      hideInSearch: true,
      dataIndex: 'jobname',
      key:'jobname'
    },
    {
      title: '职级',
      hideInSearch: true,
      dataIndex: 'jobname',
      key:'jobname'
    },
    {
      title: '离职日期',
      dataIndex: 'dimissionTime',
      hideInSearch: true,
      render: (text) => {
        return text != '-' ? moment(text).format('YYYY-MM-DD') : '';
      },
    },
    {
      title: '入职日期',
      dataIndex: 'hiredate',
      hideInSearch: true,
      render: (text) => {
        return text != '-' ? moment(text).format('YYYY-MM-DD') : '';
      },
    },
    {
      title: '审批状态',
      dataIndex: 'status',
      key:'status',
      hideInSearch: true,
      valueEnum: BPMStatusEnum,
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (item, record) => (
        <>
          {(record.status === 0) && <><a onClick={() => settleAccounts(record)}>离职结算</a></>}
          {(record.bpmDetailUrl) && <><a onClick={() => window.open(record.bpmDetailUrl)}>审批详情</a></>}
        </>
      ),
    }
  ];

  return (
    <PageHeaderWrapper>
      <ProTable
        search={false}
        actionRef={actionRef}
        rowKey="oid"
        request={(params) => querySettlementList(params)}
        columns={columns}
      />
      <SettlementForm
        record={record}
        loading={loading}
        visible={visible}
        onCancel={handleCancel}
        onSubmit={handleSubmit}
      />
    </PageHeaderWrapper>
  )
};

export default connect(({}) => ({
}))(TableList)
